﻿package com.asgamer.basics1
{

	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;

	public class SpaceJunk extends MovieClip
	{

		private var stageRef:Stage;
		private var speed:Number;
		
		public function SpaceJunk(stageRef:Stage)
		{
			this.stageRef = stageRef;
			x = Math.random()*stageRef.stageWidth;
			y = -2;
			alpha = Math.random();
			rotation = Math.random()*360;
			scaleX = Math.random();
			scaleY = Math.random();
			
			speed = 2 + Math.random()*2;
			
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
		
		public function loop(e:Event) : void 
		{
			y += speed;
			
			if (y > stageRef.stageHeight + height) {
				removeSelf();
			}
		}
		
		private function removeSelf() : void {
			
			removeEventListener(Event.ENTER_FRAME, loop);
			
			if (stageRef.contains(this) )
				stageRef.removeChild(this);
		}
		
	}
	
}